/* vim:ft=objc:
 * $Id: main.m 121 2005-05-16 19:27:17Z bmoore $
 *
 * Copyright (c) 2004, 2005  Branden J. Moore.
 *
 * This file is part of MacBiff, and is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * MacBiff is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MacBiff; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 */

#import <Cocoa/Cocoa.h>

#import <AppKit/NSNibLoading.h>

#import <MacBiffPlugIn/ssl.h>
#import <MacBiffPlugIn/Account.h>
#import <MacBiffPlugIn/Action.h>
#include <sys/types.h>
#include <limits.h>
#include <unistd.h>

#if 0
#define EBUG 1
#endif
#include <MacBiffPlugIn/debug.h>

NSMutableDictionary *accountBundles = Nil;
NSMutableDictionary *actionBundles = Nil;
NSMutableDictionary *actionPlugIns = Nil;

static NSMutableArray* bundleFiles( void )
{
	NSArray *libSearchPaths;
	NSEnumerator* searchPathEnum;
	NSString *currPath;
	NSMutableArray *bundlePaths = [NSMutableArray array];
	NSMutableArray *bundles = [NSMutableArray array];

	libSearchPaths= NSSearchPathForDirectoriesInDomains( NSLibraryDirectory,
			NSAllDomainsMask - NSSystemDomainMask, YES );
	searchPathEnum = [libSearchPaths objectEnumerator];
	while ( (currPath = [searchPathEnum nextObject]) ) {
		[bundlePaths addObject: [currPath
			stringByAppendingPathComponent: @"Application Support/MacBiff/PlugIns"]];
	}

	[bundlePaths addObject: [[NSBundle mainBundle] builtInPlugInsPath]];

	searchPathEnum = [bundlePaths objectEnumerator];

	while ( (currPath = [searchPathEnum nextObject]) ) {
		NSDirectoryEnumerator *dirEnum;
		NSString *bundlePath;

		dprintf("Searching %s for bundles\n", [currPath cString]);
		dirEnum = [[NSFileManager defaultManager]
			enumeratorAtPath: currPath];

		if ( !dirEnum ) continue;

		while ( (bundlePath = [dirEnum nextObject]) ) {
			if ( [[bundlePath pathExtension] isEqualToString: @"bundle"] ) {
				[bundles addObject: [[[NSBundle mainBundle] builtInPlugInsPath] stringByAppendingPathComponent: bundlePath]];
				dprintf("Adding Bundle %s\n", [bundlePath cString]);
			}
		}
	}
	return bundles;
}


void loadAllBundles (void)
{
	NSMutableArray *bundlePaths = bundleFiles();
	NSEnumerator *pathEnum;
	NSString *currPath;
	NSBundle *currBundle;
	Class principalClass;

	accountBundles = [[NSMutableDictionary alloc] initWithCapacity: 5];
	actionBundles = [[NSMutableDictionary alloc] initWithCapacity: 5];
	actionPlugIns = [[NSMutableDictionary alloc] initWithCapacity: 5];

	pathEnum = [bundlePaths objectEnumerator];
	while ( (currPath = [pathEnum nextObject]) ) {
		currBundle = [[NSBundle alloc] initWithPath: currPath];
		if ( currBundle ) {
			principalClass = [currBundle principalClass];
			if ( !principalClass ) continue;
			dprintf("Loading Bundle %s for type %s\n",
					[currPath cString],
					[[principalClass plugInType] cString]);
			/* Bundle appears to be valid */
			if ( ![currBundle load] ) {
				alert("Unable to load!\n");
			}
			if ( [principalClass isSubclassOfClass: [Account class]] ) {
				[accountBundles setObject: currBundle forKey: [principalClass plugInName]];
			} else if ( [principalClass isSubclassOfClass: [Action class]] )  {
				[actionBundles setObject: currBundle forKey: [principalClass plugInName]];
				[actionPlugIns setObject: [[principalClass alloc] init] forKey: [principalClass plugInName]];
			} else {
				alert("Unknown Plugin... DEATH to it!\n");
			}
		}
	}
#if defined(EBUG) && EBUG
	unsigned int i;
	NSArray *keys = [accountBundles allKeys];
	dprintf("Types found:  %d **********\n", [keys count] );
	for ( i = 0 ; i < [keys count] ; i++ ) {
		fprintf(_dbgfp, "Type %d: %s\n", i, [[keys objectAtIndex: i] cString] );
	}
#endif
}



int main(int argc, const char *argv[])
{
	int i;

	for ( i = 1 ; i < argc ; ++i ) {
		if ( !strcmp(argv[i], "-d" ) && i <= (argc-1) ) {
			print_debug = atoi(argv[++i]);
		} else if ( !strcmp( argv[i], "-f" ) ) {
			char debug_path[PATH_MAX];
			snprintf(debug_path, PATH_MAX-1, "/tmp/macbiff.%d",
					getpid());
			_dbgfp = fopen(debug_path, "w+");
			if ( !_dbgfp ) {
				_dbgfp = stderr;
				werror("Unable to open debug file");
			}
		}
	}

	if ( initialize_SSL() ) {
		error("SSL Initialization failed!\n");
	}

	return NSApplicationMain(argc, argv);
}
